package com.parse.starter.order;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.drm.ProcessedData;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.parse.PaException.PaException;
import com.parse.adapter.OrderAdapter;
import com.parse.api.OrderService;
import com.parse.api.Padelegate;
import com.parse.bean.OrderDTO;
import com.parse.bean.ProduceResultDTO;
import com.parse.contract.Args;
import com.parse.core.OrderServiceImpl;
import com.parse.starter.R;
import com.parse.starter.R.id;
import com.parse.starter.R.layout;
import com.parse.starter.R.string;

/**
 * 訂單首頁控制器
 * 
 * @author Andy
 * 
 */
public class OrderActivity extends Activity implements Padelegate {

	private ListView listview;
	final int EDIT_CODE = 001;
	private OrderDTO dto = null;
	private OrderService service;
	private ProgressDialog dialog;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == EDIT_CODE && data != null) {

			if (resultCode == RESULT_OK) {

				final ProduceResultDTO infod = //
				(ProduceResultDTO) data.getSerializableExtra(Args.BEAN_NAME);

				// 修改顯示的購買數量
				final List<ProduceResultDTO> produceInfos = //
				dto.getProduceInfos();//

				for (ProduceResultDTO info : produceInfos) {

					if (StringUtils.equals(//
							info.getObjectId(), infod.getObjectId())//

					) {
						info.setNum(infod.getNum());// 調整購買數量
					}

				}
				// 更新畫面資訊
				((BaseAdapter) listview.getAdapter()).notifyDataSetChanged();

				// 修改購買數量
				dto.getCarOfPurchasing().put(infod.getObjectId(),
						infod.getNum());

			}

		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onBackPressed() {
		if (dto.getCarOfPurchasing().size() != 0) {
			final Builder builder = new Builder(getApplicationContext());
			builder.setTitle(R.string.warm);
			builder.setMessage(R.string.HasCarPurch);
			builder.setNegativeButton(R.string.submit,
					new DialogInterface.OnClickListener() {//
						@Override
						public void onClick(DialogInterface dialog, int which) {
							onBackPressed();
						}
					})
			//
					.setNeutralButton(R.id.backBtn,
							new DialogInterface.OnClickListener() {//
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub

								}
							}).create().show();
		} else {
			super.onBackPressed();
		}
		// TODO Auto-generated method stub

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.oredr_lay);

		this.dto = new OrderDTO();

		this.service = new OrderServiceImpl();

		this.dialog = new ProgressDialog(this);
		this.dialog.setTitle(R.string.loading);

		this.listview = (ListView) findViewById(R.id.ordrrList);

		this.listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				Intent it = new Intent(OrderActivity.this, ItemActivity.class);
				// Bundle bundle = new Bundle();

				it.putExtra(Args.BEAN_NAME, dto.getProduceInfos().get(arg2));

				startActivityForResult(it, EDIT_CODE);
				// TODO Auto-generated method stub

			}

		});

		/**
		 * 發送訂單
		 */
		this.findViewById(R.id.save).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dto.setCardId("AAAAA");
				dto.setMoney("0");
				dto.setStatus("A");
				dto.setUserAdress("Taichung");
				service.doSendOrder(dto, OrderActivity.this);

			}
		});

		this.loadData();
	}

	/**
	 * 取得資料
	 */
	private void loadData() {
		this.dialog.show();// 因是背景作業
		this.service.queryProduces(dto, this);

	}

	public ListView getListview() {
		return listview;
	}

	public void setListview(ListView listview) {
		this.listview = listview;
	}

	@Override
	public void putMessage(String requestCode, Object msg, PaException e) {

		if (StringUtils.equals(requestCode, Args.COMMAND_CODE)) {
			if (e == null) {
				Toast.makeText(this, "新增訂單成功", Toast.LENGTH_SHORT).show();
				this.finish();
			} else {
				Toast.makeText(this, "新增訂單失敗", Toast.LENGTH_SHORT).show();
			}

		}

		if (StringUtils.equals(requestCode, OrderServiceImpl.class.getName())) {
			final ListView listview = (ListView) this
					.findViewById(R.id.ordrrList);
			listview.setAdapter(new OrderAdapter((OrderActivity) this, dto
					.getProduceInfos()));
			this.dialog.dismiss();

		}

	}
}
